"""
selenium 刷新回复置顶豆瓣
"""

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import code

login_url = "https://accounts.douban.com/passport/login"
account = ""
pwd = ""
a_url = "https://www.douban.com/group/topic/128036115/"
try:
    driver = webdriver.Chrome()
    driver.get(login_url)
    driver.find_element_by_class_name("account-tab-account").click()
    bsObj = BeautifulSoup(driver.page_source.encode('utf-8'),features="html.parser")
    driver.find_element_by_id("username").send_keys(account)
    driver.find_element_by_id("password").send_keys(pwd)
    driver.find_element_by_class_name("btn-active").click()
    

    time.sleep(5)
    driver.get(a_url)

    while True:

        try:

            driver.find_element_by_class_name("comment_textarea").send_keys(str(time.time()))

            #验证码
            try:
                src = driver.find_element_by_class_name("captcha_image").get_attribute("src")
                yanzhengma = code.recognize_url(src)#识别验证码
                print('识别到的验证码为:'+str(yanzhengma))
                driver.find_element_by_id("captcha_field").send_keys(yanzhengma)
                time.sleep(2)
            except Exception as e:
                print(str(e))
                src = 'nnnn'


            driver.find_element_by_name("submit_btn").click()

            #继续发言
            driver.find_element_by_id("last").click()

            time.sleep(1)
            


        except Exception as  e:
            time.sleep(20)
            driver.refresh()
        

    

except Exception as e:
    print(str(e))
    print(f"error:{e}")

driver.close()

print("ok")




